import React from 'react';
import {
    Table,
    Row,
    Col,
    Form,
    Input,
    Icon,
    InputNumber,
    Radio,
    DatePicker,
    Select,
    Upload,
    Button,
    Switch,
    Modal,
    Popconfirm,
    Tabs,
    message
} from 'antd';
import CurrencySelect from '../../../components/commons/CurrencySelect'
import AreaSelect from '../../../components/commons/AreaSelect'
import moment from 'moment'
import SingleDragger from "../../commons/SingleDragger";
import OperatorConstant from "../../../utils/OperatorConstant";
import BizParams from '../../../components/commons/BizParams'

const R = require('ramda');
const FormItem = Form.Item;
const TabPane = Tabs.TabPane;
const Dragger = Upload.Dragger;
const RadioGroup = Radio.Group;
const dateFormat = 'YYYY-MM-DD';


export default Form.create({wrappedComponentRef: true})(class EditDrawer extends React.Component {

    constructor(props) {
        super(props);

        this.state = {
            open: false,
            disabled: true,
            type: "",
            module: "drawer",
            require_memo: true,
            require_other: true,
            loading: false
        }
    }


    handleResetFields = () => {
        this.props.form.resetFields();
        if (this.refs.certInfoBussinessLicense) this.refs.certInfoBussinessLicense.resetUpload();
        if (this.refs.certInfoReceipt) this.refs.certInfoReceipt.resetUpload();
        if (this.refs.certInfoTaxRegister) this.refs.certInfoTaxRegister.resetUpload();
        if (this.refs.certInfoSaleCheck) this.refs.certInfoSaleCheck.resetUpload();
        if (this.refs.certInfoTaxplayer) this.refs.certInfoTaxplayer.resetUpload();
        if (this.refs.certInfoVATVoice) this.refs.certInfoVATVoice.resetUpload();
        if (this.refs.certInfoProtocal) this.refs.certInfoProtocal.resetUpload();
        if (this.refs.certInfoOther) this.refs.certInfoOther.resetUpload();
        if (this.refs.checkFactoryReport) this.refs.checkFactoryReport.resetUpload();
    }

    handleCancel = () => {
        this.setState({open: false, require_other: true, require_memo: true, loading: false})
        this.props.dispatch({
            type: 'drawer/setDrawer',
            payload: {
                dataSource: "",
            }
        });
        this.handleResetFields();

    }

    handleOpen = (type, disable) => {
        this.state.type = type;

        if (!R.isNil(disable)) {
            this.state.disabled = disable;
        }
        this.setState({open: true})
    }


    verification = (method) => {
        if (this.state.type == OperatorConstant.add || this.state.type == OperatorConstant.edit) {

            let certInfoReceipt = this.refs.certInfoReceipt.getUrl();
            let certInfoTaxRegister = this.refs.certInfoTaxRegister.getUrl();
            let certInfoTaxplayer = this.refs.certInfoTaxplayer.getUrl();
            let certInfoVATVoice = this.refs.certInfoVATVoice.getUrl();

            let certInfoBussinessLicense = R.isNil(this.refs.certInfoBussinessLicense) ? "" : this.refs.certInfoBussinessLicense.getUrl();
            let certInfoSaleCheck = R.isNil(this.refs.certInfoSaleCheck) ? "" : this.refs.certInfoSaleCheck.getUrl();
            let certInfoProtocal = R.isNil(this.refs.certInfoProtocal) ? "" : this.refs.certInfoProtocal.getUrl();

            let dataSource = R.isNil(this.props.dataSource) ? {} : this.props.dataSource

            let csttype = R.isNil(dataSource.csttype) ? this.props.csttype : dataSource.csttype;

            if ((R.isNil(certInfoBussinessLicense) || R.isEmpty(certInfoBussinessLicense)) && csttype !== '2') {//会员客户不要求上传
                message.error("请先上传营业执照副本照片");
                return false;
            }

            if (R.isNil(certInfoReceipt) || R.isEmpty(certInfoReceipt)) {
                message.error("请先上传销项发票照片");
                return false;
            }
            if (R.isNil(certInfoTaxRegister) || R.isEmpty(certInfoTaxRegister)) {
                message.error("请先上传税务登记证副本照片");
                return false;
            }
            if ((R.isNil(certInfoSaleCheck) || R.isEmpty(certInfoSaleCheck)) && csttype !== '2') { //会员客户不要求上传
                message.error("请先上销售验厂情况");
                return false;
            }
            if (R.isNil(certInfoTaxplayer) || R.isEmpty(certInfoTaxplayer)) {
                message.error("请先上传一般纳税人认定书");
                return false;
            }
            if (R.isNil(certInfoVATVoice) || R.isEmpty(certInfoVATVoice)) {
                message.error("请先上传进项发票照片");
                return false;
            }
            if (R.isNil(certInfoProtocal) || R.isEmpty(certInfoProtocal) && csttype !== '2') {//会员客户不要求上传
                message.error("请先上传外贸出口协议");
                return false;
            }
        }

        // if (this.state.type == OperatorConstant.approval) {
        //     let checkFactoryReport = this.refs.checkFactoryReport.getUrl();
        //     if (R.isNil(checkFactoryReport) || R.isEmpty(checkFactoryReport)) {
        //         message.error("请先上传审核附件");
        //         return false;
        //     }
        //
        // }
    }

    handleConfirm = (method) => {

        if (this.verification() == false) {
            return;
        }


        this.props.form.validateFields((errors, values) => {
            let dataSource = R.isNil(this.props.dataSource) ? {} : this.props.dataSource
            if (errors) return;

            if (this.state.type == OperatorConstant.view) {
                return;
            }


            if (this.state.type == OperatorConstant.approval) {
                if (this.state.disabled == true) {
                    return;
                }
                this.setState({loading: true});
                let checkFactoryReport = R.isNil(this.refs.checkFactoryReport) ? "" : this.refs.checkFactoryReport.getUrl();
                this.props.dispatch({
                    type: "drawer/drawerApproval",
                    payload: {
                        id: dataSource.id,
                        approvalmemo: values.approvalmemo,
                        supplygoods: values.supplygoods,
                        approvalresult: values.approvalresult,
                        islookfactory: R.isNil(values.islookfactory) ? null : values.islookfactory,
                        ticketlimit: R.isNil(values.ticketlimit) ? null : values.ticketlimit,
                        monthlylimit: R.isNil(values.monthlylimit) ? null : values.monthlylimit,
                        checkFactoryReport: checkFactoryReport,
                        onSuccess: () => {
                            this.props.handleSearch();
                            this.handleCancel();
                        },
                        onFail: () => this.setState({loading: false})
                    }
                })
            } else {
                let type = "";
                if (this.state.type == OperatorConstant.add) {
                    type = "drawer/addDrawer";


                } else if (this.state.type == OperatorConstant.edit) {
                    type = "drawer/updateDrawer";


                }

                let certInfoReceipt = this.refs.certInfoReceipt.getUrl();
                let certInfoTaxRegister = this.refs.certInfoTaxRegister.getUrl();
                let certInfoTaxplayer = this.refs.certInfoTaxplayer.getUrl();
                let certInfoVATVoice = this.refs.certInfoVATVoice.getUrl();
                let certInfoOther = this.refs.certInfoOther.getUrl();

                let certInfoBussinessLicense = R.isNil(this.refs.certInfoBussinessLicense) ? "" : this.refs.certInfoBussinessLicense.getUrl();
                let certInfoSaleCheck = R.isNil(this.refs.certInfoSaleCheck) ? "" : this.refs.certInfoSaleCheck.getUrl();
                let certInfoProtocal = R.isNil(this.refs.certInfoProtocal) ? "" : this.refs.certInfoProtocal.getUrl();


                this.props.dispatch({
                    type: type,
                    payload: {
                        method: method,
                        cstid: this.props.cstId,
                        id: dataSource.id,

                        taxplayercode: values.taxplayercode,
                        name: values.name,
                        identifitime: values.identifitime.format(dateFormat),
                        foundeddate: values.foundeddate.format(dateFormat),
                        exportvolume: values.exportvolume,
                        exportvolumecurrency: values.exportvolumecurrency,
                        workshop: values.workshop,
                        staffnum: values.staffnum,
                        financialcontactor: values.financialcontactor,
                        financialtelephone: values.financialtelephone,
                        province: R.isNil(values.area) ? null : values.area[0],
                        city: R.isNil(values.area) ? null : values.area[1],
                        district: R.isNil(values.area) ? null : values.area[2],
                        address: values.address,
                        website: values.website,
                        ischeckinfo: values.ischeckinfo,
                        goodsproperty: values.goodsproperty,
                        contactname: values.contactname,
                        contacttel: values.contacttel,
                        memo: values.memo,


                        certInfoBussinessLicense: certInfoBussinessLicense,
                        certInfoReceipt: certInfoReceipt,
                        certInfoTaxRegister: certInfoTaxRegister,
                        certInfoSaleCheck: certInfoSaleCheck,
                        certInfoTaxplayer: certInfoTaxplayer,
                        certInfoVATVoice: certInfoVATVoice,
                        certInfoProtocal: certInfoProtocal,
                        certInfoOther: certInfoOther,

                        onSuccess: () => {
                            this.props.handleSearch();
                            this.handleCancel();
                        },
                        onFail: () => this.setState({loading: false})

                    }

                })

            }

        })
    }


    handleSelect = (value) => {
        this.props.form.setFields({
            islookfactory: {errors: null},
            supplygoods: {errors: null},
            monthlylimit: {errors: null},
            approvalmemo: {errors: null},
            ticketlimit: {errors: null},
        });
        if (value == 4) {
            this.state.require_other = true;
            this.state.require_memo = false;
        } else {
            this.state.require_other = false;
            this.state.require_memo = true;
        }
    };
    visa = () => {
        return this.state.type != OperatorConstant.view
    }

    render() {
        const {getFieldDecorator} = this.props.form;
        const formItemLayout = {
            labelCol: {xs: {span: 8}, sm: {span: 6}},
            wrapperCol: {xs: {span: 15}, sm: {span: 12}}
        };

        let files;
        let footer = [];

        footer.push(<Button key="submit" size="large" loading={this.state.loading} disabled={this.props.disabled}
                            onClick={this.handleConfirm.bind(this, OperatorConstant.add)}
        >保存</Button>);

        footer.push(<Button key="cancel" size="large" onClick={this.handleCancel.bind(this, true)}>取消</Button>);

        let area = [];
        R.isNil(dataSource.province) ? "" : area.push(dataSource.province);
        R.isNil(dataSource.city) ? "" : area.push(dataSource.city);
        R.isNil(dataSource.district) ? "" : area.push(dataSource.district);


        let disableForm = this.props.disabled || this.state.type == OperatorConstant.view || this.state.type == OperatorConstant.approval;
        let disableApprovalFrom = this.state.disabled || this.state.type == OperatorConstant.view;

        return (
            <Form>
                <Row>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label="发票抬头" hasFeedback>
                            {getFieldDecorator('name', {
                                rules: [{
                                    required: true, message: '请输入发票抬头'
                                }],
                                initialValue: dataSource.name
                            })(
                                <Input disabled={disableForm}/>
                            )}
                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label="纳税人识别号" hasFeedback>
                            {getFieldDecorator('taxplayercode', {
                                rules: [{
                                    required: true, message: '请输入纳税人识别号'
                                }],
                                initialValue: dataSource.taxplayercode
                            })(
                                <Input disabled={disableForm}/>
                            )}
                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label="注册场所地址" hasFeedback>
                            {getFieldDecorator('address', {
                                rules: [{
                                    required: true, message: '请输入注册场所地址'
                                }],
                                initialValue: dataSource.address
                            })(
                                <Input maxLength="100" disabled={disableForm}/>
                            )}
                        </FormItem></Col>
                </Row>
                <Row>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label="注册固定电话" hasFeedback>
                            {getFieldDecorator('contacttel', {
                                rules: [{
                                    required: true, message: '请输入注册固定电话'
                                }],
                                initialValue: dataSource.contacttel
                            })(
                                <Input maxLength="20" disabled={disableForm}/>
                            )}
                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label="基本开户银行名称" hasFeedback>
                            {getFieldDecorator('contacttel', {
                                rules: [{
                                    required: true, message: '请输入基本开户银行名称'
                                }],
                                initialValue: dataSource.contacttel
                            })(
                                <Input maxLength="20" disabled={disableForm}/>
                            )}
                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label="基本开户账号" hasFeedback>
                            {getFieldDecorator('contacttel', {
                                rules: [{
                                    required: true, message: '请输入基本开户账号'
                                }],
                                initialValue: dataSource.contacttel
                            })(
                                <Input maxLength="20" disabled={disableForm}/>
                            )}
                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label="开票人财务联系人" hasFeedback>
                            {getFieldDecorator('financialcontactor', {
                                rules: [{
                                    required: false, message: '请输入开票人财务联系人'
                                }],
                                initialValue: dataSource.financialcontactor
                            })(
                                <Input maxLength="20" disabled={disableForm}/>
                            )}
                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label="开票人财务联系电话" hasFeedback>
                            {getFieldDecorator('financialtelephone', {
                                rules: [{
                                    required: false, message: '请输入开票人财务联系电话'
                                }],
                                initialValue: dataSource.financialtelephone
                            })(
                                <Input maxLength="20" disabled={disableForm}/>
                            )}
                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label="说明" hasFeedback>
                            {getFieldDecorator('memo', {
                                rules: [{
                                    required: false, message: '请输入说明'
                                }],
                                initialValue: dataSource.memo
                            })(
                                <Input.TextArea rows={4} maxLength="20" disabled={disableForm}/>
                            )}
                        </FormItem>
                    </Col>
                </Row>
            </Form>

        );
        let certInfoProtocal = R.isNil(attachments.certInfoProtocal) ? "" : attachments.certInfoProtocal.url;
        let certInfoOther = R.isNil(attachments.certInfoOther) ? "" : attachments.certInfoOther.url;
        let checkFactoryReport = R.isNil(attachments.checkFactoryReport) ? "" : attachments.checkFactoryReport.url;

        let csttype = R.isNil(dataSource.csttype) ? this.props.csttype : dataSource.csttype;
    }
})
